package com.qyhl.party.party.test.challenge;

import android.util.Log;

import com.qyhl.party.common.PartyUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.party.PartyChallengeBean;
import com.qyhl.webtv.commonlib.entity.party.PartyLoginBean;

public class PartyChallengeModel implements PartyChallengeContract.PartyChallengeModel {
    private PartyChallengePresenter mPresenter;

    public PartyChallengeModel(PartyChallengePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void startChallenge(String token) {
        EasyHttp.post(PartyUrl.PARTY_CHALLENGE_START)
                .headers("Authorization", token)
                .execute(new SimpleCallBack<PartyChallengeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.startError(e.getMsg());
                    }

                    @Override
                    public void onSuccess(PartyChallengeBean partyChallengeBean) {
                        mPresenter.startSuccess(partyChallengeBean);
                    }
                });
    }

    @Override
    public void submit(String token, int recordId, int questionId, String answer, int seconds) {
        EasyHttp.post(PartyUrl.PARTY_CHALLENGE_ANSWER)
                .headers("Authorization", token)
                .params("recordId", recordId + "")
                .params("questionId", questionId + "")
                .params("answer", answer)
                .params("seconds", seconds + "")
                .execute(new SimpleCallBack<PartyChallengeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.startError(e.getMsg());
                    }

                    @Override
                    public void onSuccess(PartyChallengeBean partyChallengeBean) {
                        mPresenter.submitSuccess(partyChallengeBean);
                    }
                });
    }

    @Override
    public void submitTimeOut(String token, int recordId) {
        EasyHttp.post(PartyUrl.PARTY_CHALLENGE_TIME_OUT)
                .headers("Authorization", token)
                .params("recordId", recordId + "")
                .execute(new SimpleCallBack<PartyChallengeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.startError(e.getMsg());
                    }

                    @Override
                    public void onSuccess(PartyChallengeBean partyChallengeBean) {
                        mPresenter.timeOutSuccess(partyChallengeBean);
                    }
                });
    }

    @Override
    public void isLogin() {
        EasyHttp.post(PartyUrl.PARTY_IS_LOGIN)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("phone", CommonUtils.getInstance().getUserName())
                .execute(new SimpleCallBack<PartyLoginBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 201) {
                            mPresenter.LoginError("请补充资料！");
                        } else {
                            mPresenter.LoginError(e.getMsg());
                        }

                    }

                    @Override
                    public void onSuccess(PartyLoginBean bean) {
                        mPresenter.LoginSuccess(bean);
                    }
                });
    }
}
